using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Camera : MonoBehaviour {

    // These numbers begin counting on 0, ex. If xMax is 8 then it would be 7.
    private int xMaxCoordinates = 7;
    private int yMaxCoordinates = 7;

    private int sizeOfTile = 64;
    private int borderSize = 64;

    private Texture2D textureBlack;
    private Texture2D textureWhite;

    private Vector2 hej1;

	// Use this for initialization
	void Start() {
        fitGameToScreen();
        textureBlack = createTexture(Color.black);
        textureWhite = createTexture(Color.white);

        hej1 = getVisualCoordinates(0, 0);
        Debug.Log("Normal 0,0: " + hej1);

        hej1 = getVisualCoordinatesRotated(0, 0);
        Debug.Log("Rotated 0,0: " + hej1);

        hej1 = getVisualCoordinates(7, 0);
        Debug.Log("Normal 7,0: " + hej1);

        hej1 = getVisualCoordinatesRotated(6, 0);
        Debug.Log("Rotated 6,0: " + hej1);

        hej1 = getVisualCoordinates(1, 7);
        Debug.Log("Normal 1,7: " + hej1);

        hej1 = getVisualCoordinatesRotated(2, 7);
        Debug.Log("Rotated 2,7: " + hej1);

        hej1 = getVisualCoordinates(7, 7);
        Debug.Log("Normal 7,7: " + hej1);

        hej1 = getVisualCoordinatesRotated(7, 7);
        Debug.Log("Rotated 7,7: " + hej1);
	}
	
	// Update is called once per frame
	void Update() {
	    
	}

    /// <summary>
    /// Takes x, y logical positions and returns a Vector2 in visual representation.
    /// </summary>
    /// <param name="posX">x-position</param>
    /// <param name="posY">y-position</param>
    /// <returns></returns>
    public Vector2 getVisualCoordinates(int posX, int posY)
    {
        float visualX = borderSize + posX * sizeOfTile;
        float visualY = borderSize + posY * sizeOfTile;

        return new Vector2(visualX, visualY);
    }

    /// <summary>
    /// Takes x, y logical positions and returns a Vector2 in rotated visual representation.
    /// </summary>
    /// <param name="posX">x-position</param>
    /// <param name="posY">y-position</param>
    /// <returns></returns>
    public Vector2 getVisualCoordinatesRotated(int posX, int posY)
    {
        posX = xMaxCoordinates - posX;
        posY = yMaxCoordinates - posY;

        return getVisualCoordinates(posX, posY);
    }

    public void fitGameToScreen() 
    {
        int sHeight = Screen.height;
        int sWidth = Screen.width;
        sizeOfTile = sHeight / 10;
        borderSize = sHeight / 10;
    }

    public Texture2D createTexture(Color color)
    {
        Texture2D texture = new Texture2D(1, 1);
        texture.SetPixel(0, 0, color);
        texture.Apply();
        return texture;
    }

    void OnGUI()
    {
        for (int y = 0; y <= yMaxCoordinates; y++)
        {
            for (int x = 0; x <= xMaxCoordinates; x++)
            {
                Texture2D tex;
                if (y % 2 == 0)
                {
                    tex = (x % 2 == 0 ? textureWhite : textureBlack);
                }
                else
                {
                    tex = (x % 2 == 0 ? textureBlack : textureWhite);
                }
                Vector2 position = getVisualCoordinates(x, y);
                GUI.skin.box.normal.background = tex;
                GUI.Box(new Rect(position.x, position.y, sizeOfTile, sizeOfTile), GUIContent.none);
            }
        }
    }
}
